package com.rika.controller;

import com.rika.pojo.Category;
import com.rika.pojo.ScmUser;
import com.rika.response.QueryResult;
import com.rika.response.Result;
import com.rika.service.CategoryService;
import com.rika.util.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @RequestMapping("getAllCategory")
    public QueryResult selectAllCategory(Integer currentPage,Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<Category> list=categoryService.selectAllCategory(params);
        int total=categoryService.countAll();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("addCategory")
    public Result insertCategory(Category category){
        Result result=new Result();
        categoryService.insertCategory(category);
        result.setCode(200);
        result.setMsg("添加成功");
        return  result;
    }
    @RequestMapping("deleteCategory")
    public Result deleteCategory(Integer categoryId){
        Result result=new Result();
        categoryService.deleteCategory(categoryId);
        result.setCode(200);
        result.setMsg("删除成功");
        return  result;
    }
    @RequestMapping("selectBycategoryID")
    public Result selectByCategoryId(Integer categoryId){
        Result result=new Result();
        Category category=categoryService.selectBycategoryID(categoryId);
        result.setCode(200);
        result.setData(category);
        return result;
    }
    @RequestMapping("updateCategory")
    public Result updateCategory(Category category){
        Result result=new Result();
        categoryService.updateCategory(category);
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }
    @RequestMapping("selectCategoryForProduct")
    public Result selectCategoryForProduct(){
        Result result=new Result();
        List<Category> list=categoryService.selectCategoryForProduct();
        result.setCode(200);
        result.setData(list);
        return result;
    }

}
